﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace 综合同步.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class FileController : ControllerBase
    {
        private readonly ILogger<FileController> _logger;

        public FileController(ILogger<FileController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IDictionary<string,IEnumerable<string>> GetFileList()
        {
            string basePath = LocalServer.GetConfigValue<string>("DataPath");
            return LocalServer.instance().directory.Select(o=>new DirectoryInfo(o)).Where(o=>o.Exists).
                SelectMany(o=>o.GetFiles("*.*",SearchOption.AllDirectories)).OrderBy(o=>o.CreationTime).
                Select(o=>new {
                    FileDate=o.CreationTime.ToString("yyyy年MM月dd日"),
                    FileName=o.FullName.Replace(basePath,"StaticFiles\\").Replace("\\","/")}).
                GroupBy(o=>o.FileDate).ToDictionary(o=>o.Key,t=>t.Select(o=>o.FileName));
        }
    }
}
